package xhwl.hmo.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import xhwl.hmo.mall.R;
import xhwl.hmo.mall.http.bean.resp.CardStatusResp;
import xhwl.hmo.mall.ui.SuiteProductsDetailActivity;
import xhwl.hmo.mall.utils.GlideUtil;

import static xhwl.hmo.mall.R.id.tv_basic_ori_price;

/**
 * Created by admin on 2017-10-30.
 */

public class BasicsAdapter extends BaseAdapter {
    List<CardStatusResp> cardStatuslist;
    Context context;
    String cardStatus;
    int resId=R.drawable.cardloading;
    public BasicsAdapter(Context context, List<CardStatusResp> cardStatuslist, String cardStatus) {
        this.context = context;
        this.cardStatuslist = cardStatuslist;
        this.cardStatus = cardStatus;
    }

    @Override
    public int getCount() {
        return cardStatuslist.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (holder == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_basics, null);
            holder.fra_basics = (LinearLayout) convertView.findViewById(R.id.fra_basics);
            holder.img_cardbg = (ImageView) convertView.findViewById(R.id.img_cardbg);
            holder.tv_basic_ori_price = (TextView) convertView.findViewById(tv_basic_ori_price);
            holder.tv_basic_ori_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            holder.tv_basic_salenum = (TextView) convertView.findViewById(R.id.tv_basic_salenum);
            holder.tv_basic_sal_price = (TextView) convertView.findViewById(R.id.tv_basic_sal_price);
            holder.tv_basic_jk_salenum = (TextView) convertView.findViewById(R.id.tv_basic_jk_salenum);
            if ("3".equals(cardStatus)) {
                holder.tv_basic_jk_salenum.setVisibility(View.VISIBLE);
                holder.tv_basic_salenum.setVisibility(View.GONE);
                resId=R.drawable.suitloading;
            }else{
                holder.tv_basic_jk_salenum.setVisibility(View.GONE);
                holder.tv_basic_salenum.setVisibility(View.VISIBLE);
            }
            convertView.setTag(holder);
        } else {
            convertView.getTag();
        }
        final ImageView imageView=holder.img_cardbg;
        int resId=R.drawable.cardloading;


        holder.tv_basic_jk_salenum.setText("已售" + cardStatuslist.get(position).NUM + "件");
        holder.tv_basic_salenum.setText("已售" + cardStatuslist.get(position).NUM + "件");

        if(cardStatuslist.get(position).GOODS_MONEY.equals(cardStatuslist.get(position).GOODS_SALE_MONEY)){
            holder.tv_basic_ori_price.setVisibility(View.GONE);
        }
        GlideUtil.loadIntoUseFitWidth(context,cardStatuslist.get(position).GOODS_PIC,resId,imageView);

//        Glide.with(context).load(cardStatuslist.get(position).GOODS_PIC)
//                .placeholder(R.drawable.load)
//                .error(R.drawable.load)
//                .into(imageView);

        holder.tv_basic_ori_price.setText("¥ " + cardStatuslist.get(position).GOODS_MONEY);
        holder.tv_basic_sal_price.setText("¥ " + cardStatuslist.get(position).GOODS_SALE_MONEY);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, SuiteProductsDetailActivity.class);
                intent.putExtra("GOOD_ID", cardStatuslist.get(position).GOODS_ID);
                context.startActivity(intent);
            }
        });
        return convertView;
    }
    class ViewHolder {
        ImageView img_cardbg;
        LinearLayout fra_basics;
        TextView tv_basic_ori_price, tv_basic_salenum, tv_basic_sal_price, tv_basic_jk_salenum;
    }
}
